let mask = null; // 底层透明底
let view = null; // 弹窗提示部分
let content_body = null; // 弹窗背景
let content_header = null; // 弹窗背景
let log_area = null; // 更新日志
let log = null; // 更新日志 
let btn_cancel = null;
let btn_result_cancel=null;
let btn_receive = null;
let receive_result=null;
let success_icon=null;
let systemInfo=null;
 
let util=require('./util.js')  
import api from './api.js'
import store from '@/store/index';
 
 
/*
 *  obj 参数对象
 *   systemInfo  系统参数
 *   log_data   更新日志
 *
 */

// 全民防控，发红包提示
const createdRules = (obj) =>{ 
	
	let screenWidth = obj.systemInfo.screenWidth;
	let screenHeight = obj.systemInfo.screenHeight; 
	let content_top = (obj.systemInfo.screenHeight - 250) / 2;
	let content_left = (obj.systemInfo.screenWidth - 280) / 2; 
	let msg_tip=obj.msg;
	
	// 绘制底层遮罩
	if (!mask) {
		mask = new plus.nativeObj.View('viewHotFixMask', {
			bottom: '0px',
			left: '0px',
			right: '0px',
			top: '0px',
		});
		mask.drawRect({
			color: 'rgba(0, 0, 0, 0.5)'
		});
		mask.show();
	}
	
	// 绘制弹窗背景
	if (!content_body) {
		content_body = new plus.nativeObj.View('content_body', {
			top: content_top + 'px',
			left: content_left + 'px',
			width: '280px',
			height: '250px',
			position: 'dock'
		})
		content_body.drawBitmap('/static/images/sq_model_bg.png', {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}, {
			top: 'auto',
			left: 'auto',
			width: '280px',
			height: '250px'
		});
		 
		content_body.show();
	}
	
	if (!content_header) {
		content_header = new plus.nativeObj.View('content_header', {
			top: content_top +40+ 'px',
			left: content_left + 58 + 'px',
			width: '180px',
			height: '65px',
			position: 'dock'
		}) 
		content_header.drawText(msg_tip, {
			top: '0px',
			left: '0', 
			width: '100%',
			height: '100%'
		}, {
			align: 'left',
			size: '14px',
			color: '#f00', 
			whiteSpace:'normal'
		}, ); 
	
		content_header.show();
	};
	
	// 绘制领取按钮
	if (!btn_receive) {
		btn_receive = new plus.nativeObj.View('btn_receive', {
			top: content_top + 200 + 'px',
			left: content_left + 63 + 'px',
			height: '35px',
			width: '160px',
			position: 'dock'
		}) 
		btn_receive.drawText('立即授权绑定', {
			top: '0',
			left: '0',
			width: '160px',
			height: '35px'
		}, {
			align: 'center',
			size: '14px',
			color: '#ac4100',
			weight: 'bold'
		});
		btn_receive.addEventListener("click", () => {
			uni.navigateTo({
				url:'/pages/bindPhone/bindPhone?type=bind'
			})
			Hide(); 
			// uni.login({
			//  	provider: 'weixin', 
			//     success: function (loginRes) {
			// 		console.log('微信信息',loginRes)
			// 		Hide(); 
					
			//     }
			// })
		}, false);
	
		btn_receive.show();
	}
	
	// 绘制关闭按钮
	if (!btn_cancel) {
		btn_cancel = new plus.nativeObj.View('btn_cancel', {
			top: content_top + 300 + 'px',
			left: content_left + 128 + 'px',
			height: '30px',
			width: '30px',
			position: 'dock'
		})  
		btn_cancel.drawBitmap('/static/images/close_icon.png', {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}, {
			top: 'auto',
			left: 'auto',
			height: '30px',
			width: '30px',
		}); 
			
		btn_cancel.show();
		btn_cancel.addEventListener("click", () => {
			Hide();
		}, false);
	}
}

// 全民防控，数据提交成功提示
const fkSuccessTips = (obj) =>{ 
	console.log('obj',obj.isMinData) 
	let screenWidth = obj.systemInfo.screenWidth;
	let screenHeight = obj.systemInfo.screenHeight; 
	let content_top = (obj.systemInfo.screenHeight - 250) / 2;
	let content_left = (obj.systemInfo.screenWidth - 280) / 2; 
	let isMinData=obj.isMinData;   // 学习实操剩余发布数据
	
	// 绘制底层遮罩
	if (!mask) {
		mask = new plus.nativeObj.View('viewHotFixMask', {
			bottom: '0px',
			left: '0px',
			right: '0px',
			top: '0px',
		});
		mask.drawRect({
			color: 'rgba(0, 0, 0, 0.5)'
		});
		mask.show();
	}
	
	// 绘制弹窗背景
	if (!content_body) {
		content_body = new plus.nativeObj.View('content_body', {
			top: content_top + 'px',
			left: content_left + 'px',
			width: '280px',
			height: '250px',
			position: 'dock'
		})
		content_body.drawBitmap('/static/images/sq_model_bg.png', {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}, {
			top: 'auto',
			left: 'auto',
			width: '280px',
			height: '250px'
		});
		 
		content_body.show();
	}
	
	// 绘制弹窗背景
	if (!success_icon) {
		success_icon = new plus.nativeObj.View('success_icon', {
			top: content_top + 30+'px',
			left: content_left +115+ 'px',
			width: '50px',
			height: '50px',
			position: 'dock'
		})
		success_icon.drawBitmap('/static/images/success_icon.png', {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}, {
			top: 'auto',
			left: 'auto',
			width: '50px',
			height: '50px'
		});
		 
		success_icon.show();
	}
	
	if (!content_header) {
		content_header = new plus.nativeObj.View('content_header', {
			top: content_top +80+ 'px',
			left: content_left + 58 + 'px',
			width: '180px',
			height: '65px',
			position: 'dock'
		}) 
		content_header.drawText('您的数据提交成功，请耐心等待管理员审核。', {
			top: '0px',
			left: '0', 
			width: '100%',
			height: '100%'
		}, {
			align: 'left',
			size: '14px',
			color: '#f00', 
			whiteSpace:'normal'
		}, ); 
	
		content_header.show();
	};
	
	// 绘制领取按钮
	if (!btn_receive) {
		btn_receive = new plus.nativeObj.View('btn_receive', {
			top: content_top + 200 + 'px',
			left: content_left + 63 + 'px',
			height: '35px',
			width: '160px',
			position: 'dock'
		}) 
		btn_receive.drawText('我知道了', {
			top: '0',
			left: '0',
			width: '160px',
			height: '35px'
		}, {
			align: 'center',
			size: '14px',
			color: '#ac4100',
			weight: 'bold'
		});
		btn_receive.addEventListener("click", () => {
			Hide();
			if(isMinData==1){
				store.commit('UPDATE_CERTIFICATE',1);
				uni.navigateBack({
					delta:2			 
				})
			}else{
				uni.navigateBack({
					delta:1			 
				})
			}
			
		}, false);
	
		btn_receive.show();
	}	 
}
 


const createdRedPack = (obj) => {     
	let screenWidth = obj.systemInfo.screenWidth,
		screenHeight = obj.systemInfo.screenHeight, 
		content_height = 280,
		content_top = (obj.systemInfo.screenHeight - 280) / 2,
		content_left = (obj.systemInfo.screenWidth - 310) / 2, 
		openid=obj.openid, 
		amount=obj.amount.toFixed(2), 
		order_id=obj.order_id,
		type=obj.type,
		type_name='',
		isUpdateDate='',
		isStop=true;
	if(obj.isUpdateData){
		isUpdateDate=obj.isUpdateData;
	} 
	
	if(type==1){
		type_name='全民防控';
	}else if(type==2){
		type_name='随手拍';
	}
 
	
	// 绘制底层遮罩
	if (!mask) {
		mask = new plus.nativeObj.View('viewHotFixMask', {
			bottom: '0px',
			left: '0px',
			right: '0px',
			top: '0px',
		});
		mask.drawRect({
			color: 'rgba(0, 0, 0, 0.5)'
		});
		mask.show();
	}
	
	// 绘制弹窗背景
	if (!content_body) {
		content_body = new plus.nativeObj.View('content_body', {
			top: content_top + 'px',
			left: content_left + 'px',
			width: '310px',
			height: '280px',
			position: 'dock'
		})
		content_body.drawBitmap('/static/images/redpack_bg.png', {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}, {
			top: 'auto',
			left: 'auto',
			width: '310px',
			height: '280px'
		});
		 
		content_body.show();
	};
	 
	
	if (!content_header) {
		content_header = new plus.nativeObj.View('content_header', {
			top: content_top +10+ 'px',
			left: '0px',
			width: '310px',
			height: '65px',
			position: 'dock'
		}) 
		content_header.drawText('防控红火蚁，全民共参与', {
			top: '0px',
			left: plus.os.name==="Android"?'30px':'39px', 
			width: '100%',
			height: '100%'
		}, {
			align: 'center',
			size: '16px',
			color: '#fff1c2',
			weight: 'bold'
		}, );
		content_header.drawText('恭喜您获得'+type_name+'红包', {
			top: '25px',
			left: plus.os.name==="Android"?'30px':'39px',
			width: '100%',
			height: '100%'
		}, {
			align: 'center',
			size: '14px',
			color: '#fff1c2'
		}, );
	
		content_header.show();
	};
	
	 
	 
	// 绘制领取按钮
	if (!btn_receive) {
		btn_receive = new plus.nativeObj.View('btn_receive', {
			top: content_top + 215 + 'px',
			left: content_left + 80 + 'px',
			height: '35px',
			width: '160px',
			position: 'dock'
		}) 
		btn_receive.drawText('领取红包', {
			top: '0',
			left: '0',
			width: '160px',
			height: '35px'
		}, {
			align: 'center',
			size: '14px',
			color: '#ac4100',
			weight: 'bold'
		});
		btn_receive.addEventListener("click", async (e) => {  
			if(isStop){
				isStop=false;
				// 全民防控红包
				let res=await api.yfk_api.sendRedpacketByApp({
					amount:amount,
					app_user_id:uni.getStorageSync('userInfo').id,
					order_id:order_id,
					openid:openid,  
					remark:type_name+'红包',
					type:type   // 1 全民防控红包  2 随手拍红包
				});     
				console.log('领取红包',res,{
					amount:amount,
					app_user_id:uni.getStorageSync('userInfo').id,
					order_id:order_id,
					openid:openid,  
					remark:type_name+'红包',
					type:type   // 1 全民防控红包  2 随手拍红包
				})
				if(res.data.status==1){
					plus.nativeUI.closeWaiting();
					receivedResult(obj,res.data.rdata.amount); 
					if(isUpdateDate){ 
						store.commit('UPDATE_REDPACK',1);
					}					
				}else{
					plus.nativeUI.toast(res.data.msg, {
						icon : "../static/images/error.png",// eg. "/img/add.png"
						iconWidth:"50px",
						iconHeight:"50px",
						duration : "short",// 持续3.5s，short---2s
						align : "center",// 水平居中
						verticalAlign : "center",// 垂直底部
					});
					isStop=true;
				}	
			}
					
			 
		}, false);
	
		btn_receive.show();
	}
	
	// 绘制关闭按钮
	if (!btn_cancel) {
		btn_cancel = new plus.nativeObj.View('btn_cancel', {
			top: content_top + 300 + 'px',
			left: plus.os.name==="Android"?content_left+145+ 'px':content_left+148+ 'px',
			height: '30px',
			width: '30px',
			position: 'dock'
		})  
		btn_cancel.drawBitmap('/static/images/close_icon.png', {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}, {
			top: 'auto',
			left: 'auto',
			height: '30px',
			width: '30px',
		}); 
			
		btn_cancel.show();
		btn_cancel.addEventListener("click", () => {
			Hide();
		}, false);
	}

	

};
 
 

const receivedResult = (obj,amount) => {
	
	if (content_body) {
		content_body.hide();
		content_body=null;
	}
	if (content_header) { 
		content_header.hide();
		content_header=null;
	}   
	if (btn_receive) { 
		btn_receive.hide();
		btn_receive=null;
	}
	if (btn_cancel) {
		btn_cancel.hide();
		btn_cancel=null;
	}
	
	let screenWidth = obj.systemInfo.screenWidth;
	let screenHeight = obj.systemInfo.screenHeight; 
	let content_top = (obj.systemInfo.screenHeight - 250) / 2;
	let content_left = (obj.systemInfo.screenWidth - 280) / 2; 
	// 绘制弹窗背景
	if (!content_body) {
		content_body = new plus.nativeObj.View('content_body', {
			top: content_top + 'px',
			left: content_left + 'px',
			width: '280px',
			height: '250px',
			position: 'dock'
		})
		content_body.drawBitmap('/static/images/kl_received_result.png', {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}, {
			top: 'auto',
			left: 'auto',
			width: '280px',
			height: '250px'
		});	
		 
		content_body.show();
	};
	
	
	
	if (!content_header) {
		content_header = new plus.nativeObj.View('content_header', {
			top: content_top +55+ 'px',
			left: '0px',
			width: '310px',
			height: '50px',
			position: 'dock'
		})  
		content_header.drawText('+'+amount, {
			top: '0px',
			left: '30px', 
			width: '100%',
			height: '100%'
		}, {
			align: 'center',
			size: '20px',
			color: '#e7100b',
			weight: 'bold'
		}, );
		 
		content_header.show();
	};	
	
	 
	
	// 绘制关闭按钮
	if (!btn_result_cancel) {
		btn_result_cancel = new plus.nativeObj.View('btn_result_cancel', {
			top: content_top + 198 + 'px',
			left: content_left + 63 + 'px',
			height: '35px',
			width: '160px',
			position: 'dock'
		}) 
		btn_result_cancel.drawText('我知道了', {
			top: '0',
			left: '0',
			width: '160px',
			height: '35px'
		}, {
			align: 'center',
			size: '14px',
			color: '#ac4100',
			weight: 'bold'
		});
		btn_result_cancel.addEventListener("click", () => {
			setTimeout(()=>{
				Hide();
			},1000)
			
		}, false);
	
		btn_result_cancel.show();
	}
}

const Hide = () => { 
	if (success_icon) {
		success_icon.hide();
		success_icon=null;
	}
	if (content_body) {
		content_body.hide();
		content_body=null;
	}
	if (content_header) { 
		content_header.hide();
		content_header=null;
	}
	if (mask) { 
		mask.hide();
		mask=null;
	}   
	if (btn_receive) { 
		btn_receive.hide();
		btn_receive=null;
	}
	if (btn_cancel) {
		btn_cancel.hide();
		btn_cancel=null;
	}
	if (btn_result_cancel) {
		btn_result_cancel.hide();
		btn_result_cancel=null;
	}	
}
 
 

export {
	createdRedPack,
	Hide, 
	createdRules,
	receivedResult,
	fkSuccessTips 
}
